import { acceptHMRUpdate, defineStore } from 'pinia'
import {CommonGetMediumTypeList, CommonGetTempTypeList, CommonGetUserList} from "~/requests";

const cacheKey = "__useMediumTypeStore";
export const useMediumTypeStore = defineStore('medium_type', () => {
  const options = ref<SelectProps['options']>([]);

  const loadData = async () => {
    let response = await CommonGetMediumTypeList();
    let data = response.data?.data || [];
    options.value = data.map((item) => {
      if (item['children'])
        item['children'] = Object.values(item['children']);
      return item;
    })
    storeCache();
  }

  const storeCache = () => {
    if (options.value.length) {
      localStorage.setItem(cacheKey, JSON.stringify(options.value));
    }
  }

  const restoreCache = () => {
    const data = localStorage.getItem(cacheKey);
    if (data) {
      options.value = JSON.parse(data);
    }else {
      loadData();
    }
  }

  onMounted(() => {
    restoreCache();
  })

  return {
    options,
    loadData,
  }
})

if (import.meta.hot)
  import.meta.hot.accept(acceptHMRUpdate(useMediumTypeStore as any, import.meta.hot))
